package com.region.http.client.config;

import com.region.http.client.common.Constant;
import com.region.http.client.common.ContentType;
import com.region.http.client.request.HttpExecutor;

/**
 * @author liujieyu
 * @date 2024/8/20 17:19
 * @desciption
 */
public class GlobalRequestConfig {

    private ContentType contentType;

    private int connectTime;

    private int readTime;

    private int requestTime;

    private Class<? extends HttpExecutor> executor;

    public GlobalRequestConfig(ContentType contentType, int connectTime, int readTime, int requestTime, Class<? extends HttpExecutor> executor) {
        this.contentType = contentType;
        this.connectTime = connectTime;
        this.readTime = readTime;
        this.requestTime = requestTime;
        this.executor = executor;
    }

    public ContentType getContentType() {
        return contentType;
    }

    public int getConnectTime() {
        return connectTime;
    }

    public int getReadTime() {
        return readTime;
    }

    public int getRequestTime() {
        return requestTime;
    }

    public Class<? extends HttpExecutor> getExecutor() {
        return executor;
    }

    public static GlobalRequestConfigBuilder builder() {
        return new GlobalRequestConfigBuilder();
    }

    public static final class GlobalRequestConfigBuilder {

        private ContentType contentType = ContentType.X_WWW_FORM_URLENCODED;

        private int connectTime = Constant.DEFAULT_CONNECTION_TIME;

        private int readTime = Constant.DEFAULT_READ_TIME;

        private int requestTime = Constant.DEFAULT_REQUEST_TIME;

        private Class<? extends HttpExecutor> executor = HttpExecutor.DEFAULT_HTTP_EXECUTOR_CLASS;

        public GlobalRequestConfigBuilder contentType(ContentType contentType) {
            if (contentType != ContentType.NULL) {
                this.contentType = contentType;
            }
            return this;
        }

        public GlobalRequestConfigBuilder connectTime(int connectTime) {
            if (connectTime > 0) {
                this.connectTime = connectTime;
            }
            return this;
        }

        public GlobalRequestConfigBuilder readTime(int readTime) {
            if (readTime > 0) {
                this.readTime = readTime;
            }
            return this;
        }

        public GlobalRequestConfigBuilder requestTime(int requestTime) {
            if (requestTime > 0) {
                this.requestTime = requestTime;
            }
            return this;
        }

        public GlobalRequestConfigBuilder executor(Class<? extends HttpExecutor> executor) {
            if (executor != HttpExecutor.class && HttpExecutor.class.isAssignableFrom(executor)) {
                this.executor = executor;
            }
            return this;
        }

        public GlobalRequestConfig build() {
            return new GlobalRequestConfig(contentType, connectTime, readTime, requestTime, executor);
        }
    }
}
